﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZGRemote.Server.UI.Resources;

namespace ZGRemote.Server.UI.ViewModels
{
    public partial class MainViewModel : ViewModelBase
    {
        [ObservableProperty]
        private ViewModelBase homeViewModel;

        [ObservableProperty]
        private ViewModelBase remoteViewModel;

        [ObservableProperty]
        private ViewModelBase builderViewModel;

        [ObservableProperty]
        private ViewModelBase settingsViewModel;

        [ObservableProperty]
        private ViewModelBase currentViewModel;

        public ObservableCollection<RemoteViewModelBase> RemoteViewModelList;

        public MainViewModel()
        {
            homeViewModel = new HomeViewModel();
            remoteViewModel = new RemoteViewModel();
            builderViewModel = new BuilderViewModel();
            settingsViewModel = new SettingsViewModel();
            currentViewModel = homeViewModel;
            NavigateTo(RemoteViewModel);
            RemoteViewModelList = new ObservableCollection<RemoteViewModelBase>();
        }

        [RelayCommand]
        private void Navigate(object viewModel)
        {
            switch(viewModel)
            {
                case RemoteViewModelBase vm:
                    if (vm.User?.IsConnect != true)
                    {
                        Growl.Error(Core.Properties.Resources.ClientDisconnect);
                        return;
                    }
                    else
                    {
                        NavigateTo(vm);
                    }
                    break;
                case ViewModelBase vm:
                    NavigateTo(vm);
                    break;   
            }
        }

        public void NavigateTo(ViewModelBase viewModel)
        {
            if (CurrentViewModel != viewModel) 
            { 
                CurrentViewModel.IsActivise = false;
                Header = viewModel.Header;
                CurrentViewModel = viewModel; 
                CurrentViewModel.IsActivise = true;
                viewModel.IsSelected = true;
            }
        }

        [RelayCommand]
        private void RemoveRemoteViewModel(RemoteViewModelBase viewModel)
        {
            try
            {
                RemoteViewModelList.Remove(viewModel);
                viewModel.Dispose();
            }
            catch
            {

            }
        }
    }
}
